package com.iss.apartmentmanage.controller.check;


import com.iss.apartmentmanage.entity.CheckinRecord;
import com.iss.apartmentmanage.model.MyResponseData;
import com.iss.apartmentmanage.model.RecordWithRoom;
import com.iss.apartmentmanage.model.RecordWithRoomForAll;
import com.iss.apartmentmanage.service.impl.CheckinRecordServiceImpl;
import com.iss.apartmentmanage.utils.MyJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/check")
public class CheckInRecordController {
    @Autowired
    CheckinRecordServiceImpl recordService;

    /**
     * 添加订单记录
     * @param recordWithRoom
     * @return
     */
    @RequestMapping(value = "/addCheckInRecord",method = RequestMethod.POST)
    public String addCheckInRecord(@RequestBody RecordWithRoom recordWithRoom) {
        Integer count=0 ;
        MyResponseData myResponseData = new MyResponseData();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String[] temp;
        if(recordWithRoom.getDateString()!=null){
           temp = recordWithRoom.getDateString();
            try {
                recordWithRoom.getCheckinRecord().setCheckinDate(format.parse(temp[0]));
                recordWithRoom.getCheckinRecord().setCheckoutTime(format.parse(temp[1]));
                recordWithRoom.getCheckinRecord().setRoomId(recordWithRoom.getRoom().getId());
                recordWithRoom.getCheckinRecord().setStatus(1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(recordWithRoom!=null){
            count = recordService.addCheckinRecord(recordWithRoom);


            if (count==0){
                myResponseData.setErrorCode(-1);
                myResponseData.setMsg("插入失败");
            }else {
                myResponseData.setErrorCode(1);
                myResponseData.setMsg("插入成功");
            }

        }

        return MyJson.getJson(myResponseData);
    }

    /**
     * 查询订单记录---还没有写查询计数 totalSize
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "selectAllCheckInRecord", method = RequestMethod.POST)
    public String selectAllCheckInRecord(@RequestParam Integer pageNum ,@RequestParam Integer pageSize){
        MyResponseData myResponseData = new MyResponseData();
        List<RecordWithRoomForAll> recordWithRoomForAll = recordService.selectAllCheckInRecord(pageNum, pageSize);
        if(recordWithRoomForAll!=null) {
            myResponseData.setObjectList(recordWithRoomForAll);
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("查询成功");
        }else{
            myResponseData.setErrorCode(-1);
            myResponseData.setMsg("查询失败");
        }
        return MyJson.getJson(myResponseData);
    }
    /**
     * 查询订单记录---为前端展示数据
     * @return
     */

    @RequestMapping(value = "selectAllCheckInRecordForQD", method = RequestMethod.POST)
    @ResponseBody
    public String selectAllCheckInRecordForQD(){
        String s = selectAllCheckInRecord(1,10 );
        return s;

    }

    /**
     * 查看当前房间入住的各户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectGuestMessageByRoom",method = RequestMethod.POST)
    public String selectGuestMessageByRoom(@RequestParam int id){
        MyResponseData myResponseData = new MyResponseData();
        CheckinRecord record = recordService.selectGuestMessageByRoom(id);
        myResponseData.setErrorCode(1);
        myResponseData.setObject(record);
        return MyJson.getJson(myResponseData);
    }


}
